package cn.tedu.boot41.controller;

import cn.tedu.boot41.entity.Emp;
import cn.tedu.boot41.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
public class EmpController {
    @Autowired(required = false)
    EmpMapper mapper;

    @RequestMapping("/add")
    public String add(Emp emp){
        mapper.insert(emp);
        return "添加成功";
    }
    @RequestMapping("/select")
    public String select(Emp emp){
        String html = "<center>\n" +
                "    <table border=\"1px\">\n" +
                "        <caption>英雄列表</caption>\n" +
                "        <tr>\n" +
                "            <th>id</th>\n" +
                "            <th>名字</th>\n" +
                "            <th>类型</th>\n" +
                "            <th>价格</th>\n" +
                "            <th>操作</th>\n" +
                "        </tr>";

        List<Emp> emps = mapper.select();
        for (Emp e : emps) {
            html += "<tr>\n" +
                    "        <td>"+ e.getId() +"</td>\n" +
                    "        <td>"+ e.getName() +"</td>\n" +
                    "        <td>"+ e.getJob() +"</td>\n" +
                    "        <td>"+ e.getSal() +"</td>\n" +
                    "        <td> <a href='/delete?id="+ e.getId() +"'>删除</a></td>\n" +
                    "    </tr>";
        }

        html += " </table>\n" +
                "</center>";
        return html;
    }
    @RequestMapping("/delete")
    public void delete(Integer id, HttpServletResponse response) throws IOException {
        mapper.delete(id);
        response.sendRedirect("/select");
    }
    @RequestMapping("/update")
    public void delete(Emp emp, HttpServletResponse response) throws IOException {
        mapper.update(emp);
        response.sendRedirect("/select");
    }

}
